<template>
	<view class="container pbsafe_56">
		<view class="plr_12 ptb_8 list_container">
			<view class="br_8 bc_fff mt_8 p_12" v-for="item, index in dataList" :key="index">
				<view class="fb">
					{{ item.name }}
				</view>
				<radio-group @change="radioChange">
					<label class="fx aic mt_12" :for="'radio-' + index">
						<radio :value="item.id" :id="item.id" style="transform: scale(0.7);" color="#FE5A1C"
							:checked="dealerComboId == item.id" />
						<image class="ml_4 mr_12 width_80 height_80 br_4" :src="item.url" mode="aspectFit">
						</image>
						<view class="fx_1 flow_h">
							<view class="fe_2 lh_18 height_36">
								包含{{ item.contentStr }}
							</view>
							<view class="fc_999 mt_4">
								有效期：{{ item.validityPeriod }}天
							</view>
							<view class="fc_primary fz_16 fb">
								<text class="fz_10">¥</text>{{ item.amount }}
							</view>
						</view>
					</label>
				</radio-group>
			</view>
		</view>
		<view class="pf b_0 l_0 r_0 bc_fff pbsafe_8 pt_8 plr_16">
			<button class="w100 custom_btn primary lh_40 br_56 fz_14" @click="submitData">确认购买</button>
		</view>
	</view>
</template>

<script>
import api from "@/service/config/api";
export default {
	data() {
		return {
			dataList: [],
			dealerComboId: "",
			selectProduct: {}
		}
	},
	onLoad() {
		this.getDataList()
	},
	methods: {
		getDataList() {
			api.get("/base/api/dealerComboPay/findDealerCombos").then((res) => {
				if (res && res.code == 0) {
					this.dataList = res.data
					this.dataList.map(item => {
						const contentList = item.content.split(",")
						console.log(contentList)
						const contentStrList = []
						contentList.map((value) => {
							let str = value == 1 ? "基础商城" : value == 2 ? "拼团模块" : value == 3 ?
								"秒杀模块" : value == 4 ? "抢购模块" : value == 5 ? "砍价模块" : value ==
									6 ? "红包模块" : ""
							contentStrList.push(str)
						})
						console.log(contentStrList)
						item.contentStr = contentStrList.join("、")
						console.log(item)
					})
				}
			})
		},
		radioChange(e) {
			this.dealerComboId = e.detail.value
			this.selectProduct = this.dataList.find(item => item.id == e.detail.value)
		},
		submitData() {
			const that = this
			if (!this.dealerComboId) {
				uni.$u.toast("请选择购买的套餐");
				return
			}
			const contentList = this.selectProduct.content.split(",")
			if (!contentList.includes("1")) {
				uni.$u.toast("选择购买的套餐必须包含基础商城");
				return
			}
			uni.login({
				success(res) {
					console.log(res)
					api.post("/base/api/dealerComboPay/placeOrder", {
						dealerComboId: that.dealerComboId,
						type: "APP-MINI",
						tradeType: "JSAPI",
						code: res.code
					}).then((result) => {
						console.log("支付参数")
						console.log(result)
						if (result && result.code == 0) {
							if (result.data.payParams) {

								const {
									appid,
									timeStamp,
									nonceStr,
									prepayId,
									signType,
									paySign
								} = result.data.payParams
								console.log("获取到的参数")
								console.log(result.data.payParams)
								uni.requestPayment({
									provider: 'wxpay',
									timeStamp: timeStamp,
									nonceStr: nonceStr,
									package: result.data.payParams.package,
									signType: signType,
									paySign: paySign,
									success() {
										console.log("支付成功")
										that.getUserBasicInfo()
										uni.$u.toast("购买成功");
										setTimeout(() => {
											uni.switchTab({
												url: "/pages/index/index"
											})
										}, 1500)
									},
									fail: (payFail) => {
										uni.$u.toast("购买失败");
									}
								})
							}


						}
					})
				}
			})

		},
		// 获取用户信息
		getUserBasicInfo() {
			api.get("/user/api/auth/userInfo").then((res) => {
				console.log("用户信息1");
				console.log(res);
				if (res && res.code == 0) {
					const data = res.data
					let userType = data.identityType

					if (data.identityType == 1) {
						if (data.dealer) {

							this.$storage.setItem("userType", 3);
							userType = 3
						} else if (!data.manager && !data.guide && !data.dealer) {

							this.$storage.setItem("userType", 4);
							userType = 4
						} else if (data.manager) {
							this.$storage.setItem("userType", 2);
							userType = 2

						} else if (data.guide) {
							this.$storage.setItem("userType", 1);
							userType = 1

						}

					} else if (data.identityType == 2) {
						this.$storage.setItem("userType", 3);
						userType = 3
					} else {
						this.$storage.setItem("userType", data.identityType + 2);
						userType = data.identityType + 2
					}
					console.log('userTypeuserType,', userType)
					this.$store.commit('setUserType', userType)
					if (data) {
						this.$store.commit('setUserInfo', data)
						this.$storage.setItem("basicUnserInfo", data)

					}
				}
			});
		}
	}
}
</script>
<style lang="scss">
.container {
	padding-bottom: 60rpx;
}

.list_container {
	padding-bottom: 60rpx;
}
</style>